<template>
  <div class="personal-main">
    <div class="pmain-profile">
      <div class="pmain-welcome">
        <span class="fr">上次登录时间： {{ userIndexVO.lastLoginTime }} </span>
      </div>
      <div class="pmain-user">
        <div class="user-head">
          <span class="head-img">
            <span>
              <img
                :src="userIndexVO.headImg"
                style="width:88px;height:88px;z-index:0;"
              />
              <i class="headframe" style="z-index:0;"></i>
            </span>
          </span>
        </div>
        <div class="user-info">
          <ul>
            <li>
              用户名<span>{{ userIndexVO.name }}</span>
              <NuxtLink
                v-if="
                  userIndexVO.userType === 2 && userIndexVO.bindStatus === 1
                "
                to="/user/borrower"
              >
                立即借款
              </NuxtLink>
            </li>
            <li v-if="userIndexVO.bindStatus !== 1">
              您还未开通第三方支付账户，请
              <NuxtLink to="/user/bind">立即开通</NuxtLink>
              以确保您的正常使用和资金安全。
            </li>
          </ul>
        </div>
      </div>

      <div v-if="userIndexVO.bindStatus === 1" class="pmain-money">
        <ul>
          <li class="none">
            <span>
              <em>账户余额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.amount }}</i> 元
            </span>
          </li>
          <li>
            <span>
              <em>冻结金额</em>
              <i class="markicon"></i>
            </span>
            <span class="truemoney">
              <i class="f26 fb">{{ userIndexVO.freezeAmount }}</i
              >元
            </span>
          </li>
          <li>
            <NuxtLink to="/user/recharge">
              <el-button size="mini" type="danger">充值</el-button>
            </NuxtLink>
            <NuxtLink to="/user/withdraw">
              <el-button size="mini" type="success">提现</el-button>
            </NuxtLink>
          </li>
        </ul>
      </div>
    </div>
    <el-dialog
      title="绑定手机号"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="userBind">
        <el-form-item label="手机号码" :label-width="formLabelWidth">
          <el-input v-model="userBind.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" :label-width="formLabelWidth">
          <el-select v-model="userBind.userType" placeholder="请选择">
            <el-option label="借款人" :value="2"></el-option>
            <el-option label="投资人" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleBindMobile">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userBind: {
        userType: 2,
      },
      formLabelWidth: "120px",
      dialogFormVisible: false,
      userIndexVO: {},
    };
  },

  // created() {

  // },
  mounted() {
    this.fetchUserData();
    this.isBindMobile();
  },
  methods: {
    //绑定手机号 确认按钮事件
    handleBindMobile() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.warn("请先登录");
        return;
      }
      this.$axios({
        url: "/api/core/userInfo/bindMobile",
        method: "PUT",
        data: this.userBind,
        headers: { token },
      }).then((r) => {
        this.dialogFormVisible = false;
        this.$message.success(r.data.message);
        //绑定手机号成功，删除之前缓存的userInfo
        localStorage.removeItem("userInfo");
        //绑定手机号成功立即刷新当前页面
        window.location.reload();
      });
    },
    isBindMobile() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.warn("请先登录");
        return;
      }
      this.$axios({
        url: "/api/core/userInfo/isBindMobile",
        method: "GET",
        headers: { token },
      }).then((r) => {
        this.dialogFormVisible = !r.data.data.flag;
      });
    },
    //查询会员详细数据
    fetchUserData() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message.warn("请先登录");
        return;
      }
      this.$axios({
        url: "/api/core/userInfo/index",
        method: "GET",
        headers: { token },
      }).then((r) => {
        //this.dialogFormVisible = !r.data.data.flag;
        this.userIndexVO = r.data.data.item;
      });
    },
  },
};
</script>
